package com.aierjun.notepadjun;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;

import java.util.Timer;
import java.util.TimerTask;

public class ActivityTestCountDownBtn extends Activity {
    private Button countDownBtn;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zhuce_activity);
       /* findView();*/
        initView();
    }

/*
    protected void findView() {
        countDownBtn = (Button) findViewById(R.id.get_verify_btn);
    }*/


    protected void initView() {
        countDownBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                countDownBtn.setEnabled(false);
//                countdown1();
//                countdown2();
                countdown3();
            }
        });
    }

    private void countdown1(){
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int time = 10;
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        time--;
                        if(time <= 0) {
                            timer.cancel();
                            countDownBtn.setText("重新发送");
                            countDownBtn.setEnabled(true);
                        }else {
                            countDownBtn.setText(String.format(getString(R.string.verify_btn_sended_str),time));
                        }
                    }
                });
            }
        },0,1000);
    }

    private void countdown2(){
        new CountDownBtnThread(60000,1000).start();
    }

    private void countdown3(){
        new CountdownThread(60000,1000).start();
    }

    class CountDownBtnThread extends CountDownTimer {
        private int time;

        public CountDownBtnThread(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            time = (int)(millisInFuture / countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    countDownBtn.setText(String.format(getString(R.string.verify_btn_sended_str),time--));
                }
            });

        }

        @Override
        public void onFinish() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    countDownBtn.setText("重新发送");
                    countDownBtn.setEnabled(true);
                }
            });
        }
    }


    class CountdownThread extends Thread{
        private long sumMillis;
        private long intervalMillis;
        private int time;
        public CountdownThread(long sumMillis,long intervalMillis){
            this.sumMillis = sumMillis;
            this.intervalMillis = intervalMillis;
        }

        @Override
        public synchronized void start() {
            super.start();
            this.time = (int)(sumMillis / intervalMillis);
        }

        @Override
        public void run() {
            for(;time > 0; time--){
                try {
                    Thread.sleep(intervalMillis);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        countDownBtn.setText(String.format(getString(R.string.verify_btn_sended_str),time));
                    }
                });
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    countDownBtn.setText("重新发送");
                    countDownBtn.setEnabled(true);
                }
            });
        }
    }

}
